package com.vim.modules.web.model;

import com.vim.common.model.base.BaseEntity;

import java.util.*;

public class XTree extends BaseEntity{

    /** 节点名称 */
    private String title;

    /** 节点ID */
    private String value;

    /** 父节点ID */
    private String parentValue;

    /** 选中状态 */
    private boolean checked = false;

    /** 层级 */
    private Integer level;

    /** 子节点 */
    private List<XTree> data;

    public Map<String, Object> build(List<SysMenu> allMenuList, List<SysMenu> roleMenuList){
        Map<String, Object> result = new HashMap<>();
        Map<String, XTree> xTreeMap = new LinkedHashMap<>();
        List<String> roleMenuIdList = new ArrayList<>();
        List<String> secondMenuList = new ArrayList<>();
        for(SysMenu sysMenu:roleMenuList){
            if(sysMenu.getMenuLevel() == 2){
                secondMenuList.add(sysMenu.getMenuId());
            }
            roleMenuIdList.add(sysMenu.getMenuId());
        }
        //一级菜单
        for(SysMenu sysMenu:allMenuList){
            if(sysMenu.getMenuLevel() == 1){
                XTree xTree = new XTree();
                xTree.setTitle(sysMenu.getMenuName());
                xTree.setValue(sysMenu.getMenuId());
                xTree.setParentValue(sysMenu.getMenuParentId());
                xTree.setLevel(sysMenu.getMenuLevel());
                xTree.setData(new ArrayList<>());
                if(roleMenuIdList.contains(sysMenu.getMenuId())){
                    xTree.setChecked(true);
                }
                xTreeMap.put(xTree.getValue(),xTree);
            }
        }
        //二级菜单
        for(SysMenu sysMenu:allMenuList){
            if(sysMenu.getMenuLevel() > 1){
                XTree xTree = xTreeMap.get(sysMenu.getMenuParentId());
                if(null != xTree){
                    List<XTree> subList = xTree.getData();
                    XTree sub = new XTree();
                    sub.setTitle(sysMenu.getMenuName());
                    sub.setValue(sysMenu.getMenuId());
                    sub.setParentValue(sysMenu.getMenuParentId());
                    sub.setLevel(sysMenu.getMenuLevel());
                    sub.setData(new ArrayList<>());
                    if(roleMenuIdList.contains(sysMenu.getMenuId())){
                        sub.setChecked(true);
                    }
                    subList.add(sub);
                }
            }
        }
        //拼接菜单
        List<XTree> xTreeList = new ArrayList<>();
        for(String str:xTreeMap.keySet()){
            xTreeList.add(xTreeMap.get(str));
        }

        result.put("list", xTreeList);
        result.put("select", secondMenuList);

        return result;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getParentValue() {
        return parentValue;
    }

    public void setParentValue(String parentValue) {
        this.parentValue = parentValue;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public List<XTree> getData() {
        return data;
    }

    public void setData(List<XTree> data) {
        this.data = data;
    }
}
